package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.ClientDao;
import dao.util.DBConnection;
import po.Client;
import po.Dish;

public class ClientDaoImpl implements ClientDao{
	
	Connection con = null;
	PreparedStatement ps = null;
	@Override
	public int addClient(Client c) {
		ResultSet rs = null;
		con = DBConnection.connect();
		System.out.println("con is"+con);
		String sql = "insert into c_clients(cId,cUsername,cPassword) values(c_clients_seq.nextval,?,?)";
		
		int m = 0;
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, c.getcUsername());
			ps.setString(2, c.getcPassword());
			m = ps.executeUpdate();
			return m;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return 0;
	}

	@Override
	public int delClient(int cId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Client> queryClient() {
		ResultSet rs = null;
		con = DBConnection.connect();
		String sql = "select * from c_clients";
		List<Client> listClient = new ArrayList<Client>();
		try {
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				int cId = rs.getInt("cId");
				String cUsername = rs.getString("cUsername");
				String cPassword = rs.getString("cPassword");
				Client client = new Client(cUsername, cPassword);
				client.setcId(cId);
				listClient.add(client);
			}
			return listClient;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBConnection.free(con, ps, rs);
		}
		
		return null;
	}

	@Override
	public int updateClient(Client c) {
		// TODO Auto-generated method stub
		return 0;
	}

}
